<?php

class IndexController extends CoreController
{

    public function actionHtml(){
        if(Yii::app()->request->isAjaxRequest){
            $section_code = Yii::app()->request->getParam('section_code');
            $type = Yii::app()->request->getParam('type');
            $content = CHtml::encode(Yii::app()->request->getParam('content'));
            SectionHistory::model()->createData($section_code,$type,$content);
            $data['status'] = 1;
            $data['message'] = '保存成功';
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Xtutuquan::redirect_message('非法操作','error',2);
        }
    }


	public function actionSetting()
	{
        if(Yii::app()->request->isAjaxRequest){
            $section_code = Yii::app()->request->getParam('section_code');
            $num = Yii::app()->request->getParam('num');
            $type = Yii::app()->request->getParam('type');
            $section_info = Section::model()->getFindSection($section_code);
            $section_info->num = intval($num);
            $section_info->type = $type;
            $ok = $section_info->update();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '设置成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '设置失败';
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Xtutuquan::redirect_message('非法操作','error',2);
        }
	}
    public function actionGenerate(){
        if(Yii::app()->request->isAjaxRequest){
            $section_code = Yii::app()->request->getParam('section_code');
            $section_info = Section::model()->getFindSection($section_code);
            $num = $section_info->num;
            $sr = SectionRecommend::model()->getAllData($section_code,$num);
            foreach($sr as $k=>$v){
                $sr[$k]['info'] = $v['tbl_name']::model()->getIndexInfo($v['tbl_id']);
            }

            //写入数据库
            $str = Xtutuquan::array2string($sr);
            SectionHistory::model()->createData($section_info->section_code,$section_info->type,$str);
            Yii::app()->cache->flush();
            $data['status'] = 1;
            $data['message'] = '生成成功';
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Xtutuquan::redirect_message('非法操作','error',2);
        }
    }
}